﻿using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using JIG_SoftTemplate.Common;
using JIG_SoftTemplate.Common.Services;
using JIG_SoftTemplate.Models;
using JIG_SoftTemplate.Resources.Language;
using Wpf.Ui;
using Wpf.Ui.Controls;

namespace JIG_SoftTemplate.ViewModels
{
    public partial class HomePageViewModel : ViewModelBase
    {
        private readonly Common.Services.NavigationService navigationService;
        private readonly ISnackbarService snackbarService;
        private readonly IContentDialogService contentDialogService;
        private readonly LoggerService loggerService;
        private readonly JsonConfigService jsonConfigService;

        public HomePageViewModel(Common.Services.NavigationService navigationService,
            ISnackbarService snackbarService,
            IContentDialogService contentDialogService,
            LoggerService loggerService,
            JsonConfigService jsonConfigService)
        {
            this.navigationService = navigationService;
            this.snackbarService = snackbarService;
            this.contentDialogService = contentDialogService;
            this.loggerService = loggerService;
            this.jsonConfigService = jsonConfigService;

            //注册接收广播信息的方法
            WeakReferenceMessenger.Default.Register<VMMessage>(this, (_, message) => ReceiveVMMessage(message));
        }

        /// <summary>
        /// 接受广播信息方法
        /// </summary>
        /// <param name="message">接受的VMMessage</param>
        private async Task ReceiveVMMessage(VMMessage message)
        {
            if (message.IsSpecChanged != null)
            {
                if ((bool)message.IsSpecChanged.Target)
                {
                    //读取Json配置文件
                    await LoadAppConfigAsync();
                }
            }
        }


        #region 非绑定变量
        /// <summary>
        /// 数据保存路径
        /// </summary>
        private string DataStoragePath;

        #endregion


        #region 绑定变量



        #endregion


        


        #region 私有方法

        private async Task LoadAppConfigAsync()
        {
            try
            {
                //读取配置文件
                var loadedConfig = await jsonConfigService.LoadConfigAsync(new AppConfigModel());

                if (loadedConfig != null)
                {
                    if (loadedConfig?.Common != null)
                    {
                        //更新到属性
                        DataStoragePath = loadedConfig.Common.DataStoragePath;
                    }
                    else
                    {
                        throw new Exception("Json文件Common类别读取失败");
                    }
                }

                loggerService.Info("HomePage读取配置成功");
            }
            catch (Exception ex)
            {
                loggerService.Error("HomePage读取配置出错", ex);
            }
        }

        private async Task MainProgrameAsync()
        {
            try
            {
                
            }
            catch (Exception ex)
            {
                loggerService.Error($"错误发生，出错信息：{ex.Message}");
                await App.Current.Dispatcher.InvokeAsync(() =>
                {
                    snackbarService.Show(
                        title: Lang.Snackbar_HomePage_ExTitle,
                        message: $"{Lang.Snackbar_HomePage_ExMsg}{ex.Message}",
                        appearance: Wpf.Ui.Controls.ControlAppearance.Danger,
                        icon: new SymbolIcon(SymbolRegular.Info20),
                        timeout: TimeSpan.FromSeconds(5));
                });
            }
            finally
            {
                
            }
        }
        #endregion



        #region 中继命令

        /// <summary>
        /// 载入命令
        /// </summary>
        [RelayCommand]
        public async Task Loaded()
        {
            //读取json文件中的信息
            await LoadAppConfigAsync();
            await Task.Run(async () =>
            {
                while (true)
                {
                    //通过某些条件触发
                    if (true)
                    {
                        await MainProgrameAsync();
                        await Task.Delay(1000);
                    }
                    else
                    {
                        await Task.Delay(1000);
                    }
                }
            });
        }


        /// <summary>
        /// Login按钮
        /// </summary>
        [RelayCommand]
        void BtnLogin()
        {
            navigationService.Navigate<LoginPageViewModel>();
        }

        #endregion
    }
}
